package cn.itcast.nems.studentrecord.web.controller.b;

import cn.itcast.ic.common.bean.Page;
import cn.itcast.nems.common.web.vo.BasePageRequest;
import cn.itcast.nems.journal.service.JournalFundTransferService;
import cn.itcast.nems.manager.util.RequestToEntityConverter;
import cn.itcast.nems.studentrecord.dao.condition.ApprovalType;
import cn.itcast.nems.studentrecord.dao.entity.JournalFundTransfer;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 * 学费转移凭单 前端控制器
 * </p>
 *
 * @author 陈早庆
 * @since 2024-02-23
 */
@Controller
@RequiredArgsConstructor
@RequestMapping("/b/journal/fund/transfer")
public class JournalFundTransferController {
    @Autowired
    private JournalFundTransferService journalFundTransferService;

    @GetMapping("/{rangeType}")
    public ResponseEntity<?> list(
            @PathVariable("rangeType") String rangeType,
            @RequestParam(value = "page_number", required = false, defaultValue = "0") Integer pageNumber,
            @RequestParam(value = "page_size", required = false, defaultValue = "30") Integer pageSize,
            @RequestParam(value = "id", required = false) String id,
            @RequestParam(value = "created_by", required = false) String createdBy,
            @RequestParam(value = "created_start_time", required = false) String createdStartTime,
            @RequestParam(value = "created_end_time", required = false) String createdEndTime,
            @RequestParam(value = "origin_clazz_id", required = false) Integer originClazzId,
            @RequestParam(value = "target_clazz_id", required = false) Integer targetClazzId,
            @RequestParam(value = "student_name", required = false) String studentName,
            @RequestParam(value = "student_no", required = false) String studentNo,
            @RequestParam(value = "state", required = false) String state
    ) {
        JournalFundTransfer params = new JournalFundTransfer();
        params.setId(id);
        params.setCreatedBy(createdBy);
        params.setCreatedStartTime(createdStartTime);
        params.setCreatedEndTime(createdEndTime);
        params.setOriginClazzId(originClazzId);
        params.setTargetClazzId(targetClazzId);
        params.setStudentName(studentName);
        params.setStudentNo(studentNo);
        params.setState(state);
        params.setApprovalType(ApprovalType.valueOf(rangeType.toUpperCase()));
        // TODO 分页查询
        Page<JournalFundTransfer> aPage = journalFundTransferService.listPage(pageNumber, pageSize, params);
        return ResponseEntity.ok().body(aPage);
    }

    // 班級下拉
    @GetMapping(value = "/clazzes/{rangeType}")
    public ResponseEntity<Page<Map>> searchClazzesCommit(@PathVariable("rangeType") String rangeType,
                                                         @RequestParam(value = "page_number", required = false) Integer pageNumber,
                                                         @RequestParam(value = "page_size", required = false) Integer pageSize,
                                                         @RequestParam(value = "id", required = false) String id,
                                                         @RequestParam(value = "created_by", required = false) String createdBy,
                                                         @RequestParam(value = "created_start_time", required = false) String createdStartTime,
                                                         @RequestParam(value = "created_end_time", required = false) String createdEndTime,
                                                         @RequestParam(value = "origin_clazz_name", required = false) String originClazzName,
                                                         @RequestParam(value = "target_clazz_name", required = false) String targetClazzName,
                                                         @RequestParam(value = "student_name", required = false) String studentName,
                                                         @RequestParam(value = "student_no", required = false) String studentNo) {
        JournalFundTransfer params = new JournalFundTransfer();
        params.setId(id);
        params.setCreatedBy(createdBy);
        params.setCreatedStartTime(createdStartTime);
        params.setCreatedEndTime(createdEndTime);
        params.setOriginClazzName(originClazzName);
        params.setTargetClazzName(targetClazzName);
        params.setStudentName(studentName);
        params.setStudentNo(studentNo);
        params.setApprovalType(ApprovalType.valueOf(rangeType.toUpperCase()));
        return ResponseEntity.ok(journalFundTransferService.searchClazzes(pageNumber, pageSize, params));
    }

    @GetMapping(value = "/get/{id}")
    public ResponseEntity<JournalFundTransfer> getById(@PathVariable("id") String id) {
        return ResponseEntity.ok().body(journalFundTransferService.getById(id));
    }

    @PostMapping
    public ResponseEntity<String> create(@RequestBody JournalFundTransfer params) {
        return ResponseEntity.ok().body(journalFundTransferService.create(params));
    }

    @DeleteMapping(value = "/{id}")
    public ResponseEntity<Boolean> delete(@PathVariable("id") String id) {
        return ResponseEntity.ok().body(journalFundTransferService.delete(id));
    }

    @PutMapping
    public ResponseEntity<Boolean> update(@RequestBody JournalFundTransfer params) {
        return ResponseEntity.ok().body(journalFundTransferService.update(params));
    }

    /**
     * 提交
     *
     * @param id 升级凭单ID
     * @return 是否成功
     **/
    @PutMapping("/flow/{id}")
    public ResponseEntity<Boolean> flow(@PathVariable("id") String id) {
        return ResponseEntity.ok(journalFundTransferService.flow(id));
    }
}
